from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import smtplib
import time
import os

def send_mail(file):
    f = open(file, 'rb')
    mail_body = f.read()

    sender = '2499097859@qq.com' #发邮件的邮箱地址
    mailto = '862304873@qq.com' # 收邮件地址
    filelist=file.split("/")
    reportfile=filelist[-1]

    att = MIMEText(mail_body,'base64','utf-8')
    att['Content-Type'] = 'application/octet-stream'
    att['Content-Disposition'] = 'attachment; filename='+"%s"%reportfile
    msg = MIMEMultipart('related')
    msg['Subject'] = '菜篮子接口自动化测试报告:'+reportfile  #邮件标题
    # msg=MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
    msg.attach(att)
    msg['From'] = sender
    msg['To'] = mailto
    
    smtp = smtplib.SMTP()
    smtp.connect("smtp.qq.com")
    smtp.login("2499097859@qq.com", "skzkpsiqkixwebih")
    smtp.sendmail("2499097859@qq.com", "862304873@qq.com", msg.as_string())
    smtp.quit()
    print("\n测试报告已经发送！")
    f.close

if __name__ == '__main__':
    send_mail("")
